package com.mmall.concurrency.example.other;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author:LiuWei
 * @Description:
 * @Date: Created in 14:58 2019/3/19
 * @Modified By:
 */
@Slf4j
public class CyclicBarrierTest{
    public static CyclicBarrier c = new CyclicBarrier(5);
    public static int threadTotal = 4;
    public static ExecutorService executorService = Executors.newFixedThreadPool(threadTotal);
    public static Semaphore semaphore = new Semaphore(4);
    public static AtomicInteger countInteger = new AtomicInteger(0);
    public static void main(String[] args) throws  Exception {
        for(int i = 0;i<threadTotal;i++){
            final int index = i;
            executorService.submit(()->{
               try{
                   c.await();
                   semaphore.acquire();
                   System.out.println("Thread-"+countInteger.getAndDecrement());
                   semaphore.release();
               }catch (Exception e){
                   log.error(e.getMessage());
               }
            });
        }
        c.await();
        executorService.shutdown();
        System.out.println("thread pool end");
    }
}
